<?php
namespace Backend\Controllers\Car;

use Backend\Controllers\BaseController;
use Backend\Models\Car\ServerRole;

/**
 * @RoutePrefix("/car/serverrole")
 */
class ServerRoleController extends BaseController
{
    public function initialize()
    {
        return parent::initialize();
    }

    /**
     * @Post(
     *     "/gets"
     * )
     */
    public function getsAction()
    {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('pageSize')
        ];
        if ($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的页数！']);
        }
        if ($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的每页的条数！']);
        }
        $data['size'] = intval($data['size']);
        $data['page'] = intval($data['page']);
        $offset       = intval(($data['page'] - 1) * $data['size']);
        $model        = new ServerRole();
        $list         = $model->gets($data['size'], $offset);
        if ($list !== false) {
            return $this->json([
                'code' => 200,
                'msg'  => '获取成功!',
                'list' => $list
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }

    /**
     * @Post(
     *     "/add"
     * )
     */
    public function addAction()
    {
        $data = [
            'name'   => $this->request->getPost('name'),
            'status' => $this->request->getPost('status')
        ];
        if ($data['name'] == '') {
            return $this->json([
                'code' => 501,
                'msg'  => '角色名称不能为空～'
            ]);
        }

        if ($data['status'] == '' || !is_numeric($data['status'])) {
            return $this->json([
                'code' => 501,
                'msg'  => '状态不能为空～'
            ]);
        }
        $model = new ServerRole();
        if ($model->add($data)) {
            return $this->json(['code' => 200, 'msg' => '添加成功～']);
        }
        return $this->json([
            'code' => 501,
            'msg'  => '系统错误～'
        ]);
    }

    /**
     * @Post(
     *     "/edit"
     * )
     */
    public function editAction()
    {
        $data = [
            'id'     => $this->request->getPost('id'),
            'name'   => $this->request->getPost('name'),
            'status' => $this->request->getPost('status')
        ];
        if ($data['id'] == '' || !is_numeric($data['id'])) {
            return $this->json([
                'code' => 501,
                'msg'  => 'id丢失～'
            ]);
        }
        if ($data['name'] == '') {
            return $this->json([
                'code' => 501,
                'msg'  => '名称不能为空～'
            ]);
        }
        if ($data['status'] == '' || !is_numeric($data['status'])) {
            return $this->json([
                'code' => 501,
                'msg'  => '状态不能为空～'
            ]);
        }
        $model = new ServerRole();
        if ($model->edit($data)) {
            return $this->json(['code' => 200, 'msg' => '编辑成功～']);
        }
        return $this->json([
            'code' => 501,
            'msg'  => '系统错误～'
        ]);
    }

    /**
     * @Post(
     *     "/getCount"
     * )
     */
    public function getCountAction()
    {
        $cnt = (new ServerRole())->getCount();
        if ($cnt === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        return $this->json([
            'code'  => 200,
            'count' => $cnt
        ]);
    }

    /**
     * @Post(
     *     "/get"
     * )
     */
    public function getAction()
    {
        $id = $this->request->getPost('id');
        if ($id == '' || !is_numeric($id)) {
            return $this->json([
                'code' => 501,
                'msg'  => '参数丢失～'
            ]);
        }
        $model = new ServerRole();
        $ret   = $model->get($id);
        if ($ret !== false) {
            return $this->json([
                'code' => 200,
                'ret'  => $ret
            ]);
        }
        return $this->json([
            'code' => 501,
            'msg'  => '系统错误，请稍候再试～'
        ]);
    }
}
